package com.aidong.video.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.util.PageUtil;
import com.aidong.video.biz.TrnVideoBiz;
import com.aidong.video.entity.FoodList;
import com.aidong.video.entity.TrnVideo;
import com.aidong.video.entity.VideoContent;
import com.aidong.video.entity.VideoCourse;
import com.aidong.video.entity.VideoHome;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class TrnVideoAction implements ModelDriven<TrnVideo> {
	private TrnVideo trnVideo;
	
	@Override
	public TrnVideo getModel() {
		if (trnVideo == null) {
			trnVideo = new TrnVideo();
		}
		return trnVideo;
	}
	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	private TrnVideoBiz trnVideoBiz; 
	@Autowired
	public void setTrnVideoBiz(TrnVideoBiz trnVideoBiz) {
		this.trnVideoBiz = trnVideoBiz;
	} 
	
	
	/**
	 * 视频首页列表
	 * 
	 * @return
	 */
	@Action(value = "getVideoList", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getVideoList() {
		map = new HashMap<String, Object>();
		if(!StringUtils.isBlank(trnVideo.getList())){
			try{
				Map<String, Object> pageMap = PageUtil.page(20, trnVideo.getPageCurrent());
				pageMap.put("cityId", trnVideo.getCityId());
				int videoType=0;
				
				if("professional".equals(trnVideo.getList())){
					videoType=1;//专题
				}else if("celebrity".equals(trnVideo.getList())){
					videoType=2;//名人
				}else if("family".equals(trnVideo.getList())){
					videoType=3;//深入
				}
				pageMap.put("videoType", videoType);	
				List<VideoHome> videoHome=new ArrayList<VideoHome>();
				if("professional".equals(trnVideo.getList())){
					//专题
					videoHome=trnVideoBiz.getVideoList(pageMap);
				}else{
					//名人和深入
					videoHome=trnVideoBiz.getVideoListOther(pageMap);
				}
				Map<String,Object> resultMap=new HashMap<String,Object>();
				resultMap.put("video", videoHome);
				map.put("code", 1);
				map.put("result", resultMap);
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	
	/**
	 * 单个视频详情
	 * 
	 * @return
	 */
	@Action(value = "getVideoDetail", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getVideoDetail() {
		map = new HashMap<String, Object>();
		if(trnVideo.getVideoId() != 0 && trnVideo.getPhase() !=0){
			try{
				Map<String, Object> pageMap =new HashMap<String,Object>();
				pageMap.put("videoId", trnVideo.getVideoId());
				pageMap.put("phase", trnVideo.getPhase());	
				map.put("code", 1);
				map.put("result", trnVideoBiz.getVideoDetail(pageMap));
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	/**
	 * 视频详情列表
	 * @return
	 */
	@Action(value = "getVideoDetailList", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getVideoDetailList() {
		map = new HashMap<String, Object>();
		if(trnVideo.getVideoId() != 0){
			try{
				Map<String, Object> pageMap =new HashMap<String,Object>();
				pageMap.put("videoId", trnVideo.getVideoId());
				List<VideoContent> videoList=trnVideoBiz.getVideoDetailList(pageMap);
				List<VideoContent> praiseList=trnVideoBiz.getVideoPraiseList(pageMap);
				for(int i=0;i<videoList.size();i++){
					try{
						videoList.get(i).setLikesCount(praiseList.get(i).getLikesCount());
						videoList.get(i).setCommentsCount(praiseList.get(i).getCommentsCount());
					}catch(Exception e){
						e.printStackTrace();
					}
				}
				map.put("code", 1);
				map.put("result",videoList);
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	/**
	 * 视频相关
	 * @return
	 */
	@Action(value = "getVideoRelations", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getVideoRelations() {
		map = new HashMap<String, Object>();
		if(trnVideo.getVideoId() > 0 && trnVideo.getPhase() > 0){
			try{
				Map<String, Object> pageMap =new HashMap<String,Object>();
				pageMap.put("videoId", trnVideo.getVideoId());
				pageMap.put("phase", trnVideo.getPhase());
				pageMap.put("cityId", trnVideo.getCityId());
				List<FoodList> foodList=trnVideoBiz.getVideoFoodList(pageMap);
				List<VideoCourse> courseList=trnVideoBiz.getVideoCourseList(pageMap);
				List<VideoContent> contentList=trnVideoBiz.getVideoRList(pageMap);
				Map<String,Object> resultMap=new HashMap<String,Object>();
				resultMap.put("good", foodList);
				resultMap.put("course", courseList);
				resultMap.put("video", contentList);
				map.put("code", 1);
				map.put("result", resultMap);
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	/**
	 *播放视频时记录人员
	 * 
	 * @return
	 */
	@Action(value = "addVideoMsg", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String addLivingMsg() {
		map = new HashMap<String, Object>();
		if(trnVideo.getVideoId() > 0){
			try{
				Map<String, Object> pageMap =new HashMap<String,Object>();
				pageMap.put("videoId", trnVideo.getVideoId());
				pageMap.put("idongId", trnVideo.getIdongId());
				trnVideoBiz.addVideoPersion(pageMap);
				map.put("code", 1);
				map.put("result", "添加成功");
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
}
